package com.xie.controller;

import com.xie.pojo.Teacher;
import com.xie.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    @Qualifier("TeacherServiceImpl")
    private TeacherService teacherService;


    @RequestMapping("/allTeacher")
    public String allTeacher(HttpSession session){
        List<Teacher> teachers = teacherService.queryAllTeacher();
        session.setAttribute("teacherList",teachers);
        return "teacher";
    }

    @RequestMapping("/deleteTeacher")
    public String deleteTeacher(String tid, Model model){
        int i = teacherService.deleteTeacher(tid);
        if(i > 0){
            return "redirect:/teacher/allTeacher";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/teacher/allTeacher";
        }
    }

    @RequestMapping("/toAddTeacher")
    public String toAddTeacher(){
        return "addTeacher";
    }
    @RequestMapping("/addTeacher")
    public String addTeacher(Teacher teacher,Model model){
        int i = teacherService.addTeacher(teacher);
        if (i > 0){
            return "redirect:/teacher/allTeacher";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/teacher/toAddTeacher";
        }
    }

    @RequestMapping("/toUpdateTeacher")
    public String toUpdateTeacher(String tid,Model model){
        Teacher teacher = teacherService.queryTeacher(tid);
        model.addAttribute("QTeacher",teacher);
        return "updateTeacher";
    }
    @RequestMapping("/updateTeacher")
    public String updateTeacher(Teacher teacher,Model model){
        int i = teacherService.updateTeacher(teacher);
        if (i > 0){
            return "redirect:/teacher/allTeacher";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/teacher/allTeacher";
        }
    }












}
